package view.student;

import javax.swing.*;
import java.awt.*;

/**
 * 预约规则面板
 */
public class ReservationRulesPanel extends JPanel {

        public ReservationRulesPanel() {
                initComponents();
        }

        private void initComponents() {
                setLayout(new BorderLayout());
                setBackground(new Color(248, 250, 252));
                setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));

                // 顶部面板
                JPanel topPanel = createTopPanel();
                add(topPanel, BorderLayout.NORTH);

                // 主要内容面板
                JPanel mainPanel = createMainContentPanel();
                add(mainPanel, BorderLayout.CENTER);
        }

        /**
         * 创建顶部面板
         */
        private JPanel createTopPanel() {
                JPanel topPanel = new JPanel(new BorderLayout());
                topPanel.setBackground(new Color(248, 250, 252));
                topPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 25, 0));

                // 标题区域
                JPanel titleArea = new JPanel();
                titleArea.setLayout(new BoxLayout(titleArea, BoxLayout.Y_AXIS));
                titleArea.setBackground(new Color(248, 250, 252));

                JLabel titleLabel = new JLabel("自习室预约规则");
                titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 28));
                titleLabel.setForeground(new Color(31, 41, 55));
                titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

                JLabel subtitleLabel = new JLabel("请仔细阅读以下规则，了解新的考勤签到和信誉分制度");
                subtitleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
                subtitleLabel.setForeground(new Color(107, 114, 128));
                subtitleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
                subtitleLabel.setBorder(BorderFactory.createEmptyBorder(8, 0, 0, 0));

                titleArea.add(titleLabel);
                titleArea.add(subtitleLabel);

                // 图标装饰
                JLabel iconLabel = new JLabel("📋");
                iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 48));
                iconLabel.setHorizontalAlignment(SwingConstants.CENTER);

                topPanel.add(titleArea, BorderLayout.WEST);
                topPanel.add(iconLabel, BorderLayout.EAST);

                return topPanel;
        }

        /**
         * 创建主要内容面板
         */
        private JPanel createMainContentPanel() {
                JPanel mainPanel = new JPanel(new GridLayout(2, 2, 20, 20));
                mainPanel.setBackground(new Color(248, 250, 252));

                // 创建六个规则卡片
                mainPanel.setLayout(new GridLayout(3, 2, 20, 20));

                mainPanel.add(createRuleCard("📖 如何预约",
                                new String[] { "选择【座位预约】菜单，查看座位布局图",
                                                "选择空闲座位，设置预约时间（2-8小时）",
                                                "确认预约信息并提交完成预约" },
                                new Color(59, 130, 246)));

                mainPanel.add(createHighlightCard("⏰ 预约时间规则",
                                new String[] { "开放时间：08:00-22:00，预约时长2-8小时",
                                                "【重点】需在预约时间内完成签到和签退",
                                                "签到窗口：开始前15分钟到开始后30分钟" },
                                new Color(34, 197, 94), true));

                mainPanel.add(createRuleCard("📋 预约限制",
                                new String[] { "每人同时最多预约1个座位，每日限一次",
                                                "必须实名预约，不得代他人预约",
                                                "预约后请按时到达，建议提前30分钟" },
                                new Color(245, 158, 11)));

                mainPanel.add(createHighlightCard("✅ 考勤签到规则",
                                new String[] { "【重点】预约后必须在指定时间内签到和签退",
                                                "迟到超过30分钟扣5分，早退超过30分钟扣3分",
                                                "系统会自动处理爽约行为，扣除10分" },
                                new Color(168, 85, 247), true));

                mainPanel.add(createRuleCard("🏛️ 自习室规则",
                                new String[] { "保持安静禁止喧哗，座位整洁禁止占座",
                                                "禁止在自习室内进食，可带水杯",
                                                "贵重物品请随身携带，遗失概不负责" },
                                new Color(147, 51, 234)));

                mainPanel.add(createRuleCard("⭐ 信誉分制度",
                                new String[] { "初始信誉分100分，正常使用+1分，爽约-10分",
                                                "信誉分低于20分将被禁用预约7天",
                                                "连续7天正常使用额外奖励5分" },
                                new Color(239, 68, 68)));

                return mainPanel;
        }

        /**
         * 创建规则卡片
         */
        private JPanel createRuleCard(String title, String[] rules, Color accentColor) {
                JPanel card = new JPanel();
                card.setLayout(new BorderLayout());
                card.setBackground(Color.WHITE);
                card.setBorder(BorderFactory.createCompoundBorder(
                                new RoundedBorder(12),
                                BorderFactory.createEmptyBorder(15, 15, 15, 15)));

                // 添加阴影效果（通过多层边框模拟）
                card.setBorder(BorderFactory.createCompoundBorder(
                                BorderFactory.createCompoundBorder(
                                                BorderFactory.createMatteBorder(0, 0, 2, 2, new Color(0, 0, 0, 30)),
                                                BorderFactory.createMatteBorder(0, 0, 1, 1, new Color(0, 0, 0, 20))),
                                BorderFactory.createCompoundBorder(
                                                new RoundedBorder(12),
                                                BorderFactory.createEmptyBorder(25, 25, 25, 25))));

                // 标题
                JLabel titleLabel = new JLabel(title);
                titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
                titleLabel.setForeground(accentColor);
                titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));

                // 规则内容
                JPanel rulesPanel = new JPanel();
                rulesPanel.setLayout(new BoxLayout(rulesPanel, BoxLayout.Y_AXIS));
                rulesPanel.setBackground(Color.WHITE);

                for (String rule : rules) {
                        JPanel ruleItemPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 2));
                        ruleItemPanel.setBackground(Color.WHITE);

                        JLabel bulletLabel = new JLabel("•");
                        bulletLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
                        bulletLabel.setForeground(accentColor);
                        bulletLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 8));

                        JLabel ruleLabel = new JLabel(rule);
                        ruleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
                        ruleLabel.setForeground(new Color(75, 85, 99));

                        ruleItemPanel.add(bulletLabel);
                        ruleItemPanel.add(ruleLabel);
                        rulesPanel.add(ruleItemPanel);
                }

                card.add(titleLabel, BorderLayout.NORTH);
                card.add(rulesPanel, BorderLayout.CENTER);

                return card;
        }

        /**
         * 创建重点突出的规则卡片
         */
        private JPanel createHighlightCard(String title, String[] rules, Color accentColor, boolean highlight) {
                JPanel card = new JPanel();
                card.setLayout(new BorderLayout());
                card.setBackground(Color.WHITE);

                // 重点卡片使用更明显的边框和阴影
                if (highlight) {
                        card.setBorder(BorderFactory.createCompoundBorder(
                                        BorderFactory.createCompoundBorder(
                                                        BorderFactory.createCompoundBorder(
                                                                        BorderFactory.createMatteBorder(0, 0, 3, 3,
                                                                                        new Color(0, 0, 0, 50)),
                                                                        BorderFactory.createMatteBorder(0, 0, 2, 2,
                                                                                        new Color(0, 0, 0, 30))),
                                                        BorderFactory.createLineBorder(accentColor, 2)),
                                        BorderFactory.createEmptyBorder(15, 15, 15, 15)));
                } else {
                        card.setBorder(BorderFactory.createCompoundBorder(
                                        BorderFactory.createCompoundBorder(
                                                        BorderFactory.createMatteBorder(0, 0, 2, 2,
                                                                        new Color(0, 0, 0, 30)),
                                                        BorderFactory.createMatteBorder(0, 0, 1, 1,
                                                                        new Color(0, 0, 0, 20))),
                                        BorderFactory.createCompoundBorder(
                                                        new RoundedBorder(12),
                                                        BorderFactory.createEmptyBorder(15, 15, 15, 15))));
                }

                // 标题
                JLabel titleLabel = new JLabel(title);
                titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
                titleLabel.setForeground(accentColor);
                titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));

                // 规则内容
                JPanel rulesPanel = new JPanel();
                rulesPanel.setLayout(new BoxLayout(rulesPanel, BoxLayout.Y_AXIS));
                rulesPanel.setBackground(Color.WHITE);

                for (String rule : rules) {
                        JPanel ruleItemPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 2));
                        ruleItemPanel.setBackground(Color.WHITE);

                        JLabel bulletLabel = new JLabel("•");
                        bulletLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
                        bulletLabel.setForeground(accentColor);
                        bulletLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 8));

                        JLabel ruleLabel = new JLabel(rule);

                        // 重点内容突出显示
                        if (rule.contains("【重点】")) {
                                ruleLabel.setText(rule.replace("【重点】", ""));
                                ruleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
                                ruleLabel.setForeground(new Color(185, 28, 28)); // 更鲜明的红色

                                // 添加重点标识
                                JLabel highlightLabel = new JLabel("【重点】");
                                highlightLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
                                highlightLabel.setForeground(new Color(239, 68, 68));
                                highlightLabel.setBackground(new Color(254, 242, 242));
                                highlightLabel.setOpaque(true);
                                highlightLabel.setBorder(BorderFactory.createEmptyBorder(2, 6, 2, 6));

                                ruleItemPanel.add(highlightLabel);
                                ruleItemPanel.add(Box.createHorizontalStrut(5));
                        } else {
                                ruleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
                                ruleLabel.setForeground(new Color(75, 85, 99));
                        }

                        ruleItemPanel.add(bulletLabel);
                        ruleItemPanel.add(ruleLabel);
                        rulesPanel.add(ruleItemPanel);
                }

                card.add(titleLabel, BorderLayout.NORTH);
                card.add(rulesPanel, BorderLayout.CENTER);

                return card;
        }

        /**
         * 圆角边框类
         */
        private static class RoundedBorder implements javax.swing.border.Border {
                private int radius;

                public RoundedBorder(int radius) {
                        this.radius = radius;
                }

                @Override
                public Insets getBorderInsets(Component c) {
                        return new Insets(2, 2, 2, 2);
                }

                @Override
                public boolean isBorderOpaque() {
                        return false;
                }

                @Override
                public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
                        Graphics2D g2d = (Graphics2D) g.create();
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                        g2d.setColor(new Color(229, 231, 235));
                        g2d.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
                        g2d.dispose();
                }
        }
}
